﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Data;

namespace ConfExpert.Forms.Depo.Westbrook
{
    public partial class SoldQuantitiesPOS : XForm
    {
        public SoldQuantitiesPOS()
        {
            InitializeComponent();
        }

        private void SoldQuantitiesPOS_Load(object sender, EventArgs e)
        {
            gridView1.Init("GetSoldQuantitiesPOS");
            gridView1.AddDetailGrid(gridView2, "COStyleId");

            gridView2.MakeColumnInvisible("COModelId", "COOrderId");

            AddScan(gridView2, "S1", "S2", "S3", "S4");
            gridView2.SetCaptions("S1:Sc1", "S2:Sc2", "S3:Sc3", "S4:Sc4", "Total:Total Pcs");
            gridView2.YanYanaGetirSoluna("Cons4",
                "S1", "Product1Id", "Cons1",
                "S2", "Product2Id", "Cons2",
                "S3", "Product3Id", "Cons3",
                "S4", "Product4Id");


            gridView1.FormatAsNumber(0, "Eksik");
            gridView1.AddSummary("Eksik");

            gridView2.CustomUnboundColumnData -= gridView2_CustomUnboundColumnData;
            gridView2.CalcRowHeight -= gridview_CalcRowHeight;

            gridView2.CustomUnboundColumnData += gridView2_CustomUnboundColumnData;
            gridView2.CalcRowHeight += gridview_CalcRowHeight;

            gridView2.MakeReadOnly("ProductId", "Cons0", "Product1Id", "Cons1", "Product2Id", "Cons2", "Product3Id", "Cons3", "Product4Id", "Cons4");
            gridView2.FormatAsNumber(0, "Total");
            gridView2.AddSummary("Total");

            btnKaydet.ItemClick += btnKaydet_ItemClick;

            gridView2.RowStyle -= gridView2_RowStyle;
            gridView2.RowStyle += gridView2_RowStyle;

            gridView2.RowCellClick+=gridView2_RowCellClick;
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();

            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gridView2.BestFitColumns();
        }

        void gridView2_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            GridView gv = sender as GridView;
            switch (gv.FocusedColumn.FieldName)
            {
                case "S1": Goster(gv.GetFocusedRowCellValue("Product1Id").ToInt()); break;
                case "S2": Goster(gv.GetFocusedRowCellValue("Product2Id").ToInt()); break;
                case "S3": Goster(gv.GetFocusedRowCellValue("Product3Id").ToInt()); break;
                case "S4": Goster(gv.GetFocusedRowCellValue("Product4Id").ToInt()); break;
            }
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView2.EndEditing();
            gridView2.Update("COModelId,ProductId,Cons0,Product1Id,Cons1,Product2Id,Cons2,Product3Id,Cons3,Product4Id,Cons4", "COModel");
            gridView2.RefreshDataSource("GetSoldQuantitiesPOS");

            Notify("Saved.");
        }

        void gridView2_RowStyle(object sender, RowStyleEventArgs e)
        {
            DataRow r = ((GridView)sender).GetDataRow(e.RowHandle);
            if (r != null)
                if (r["Eksik"].ToStr() == "E")
                    e.Appearance.ForeColor = System.Drawing.Color.SlateGray;
        }

        void AddScan(GridView gridview, params string[] FieldNames)
        {
            foreach (string FieldName in FieldNames)
            {
                GridColumn colPhoto = new GridColumn();
                colPhoto.FieldName = FieldName;
                gridview.Columns.Add(colPhoto);

                colPhoto.ColumnEdit = new RepositoryItemPictureEdit();
                colPhoto.UnboundType = DevExpress.Data.UnboundColumnType.Object;

                (colPhoto.ColumnEdit as RepositoryItemPictureEdit).SizeMode = PictureSizeMode.Clip;
                (colPhoto.ColumnEdit as RepositoryItemPictureEdit).NullText = " ";
            }
        }


        public void gridView2_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.ListSourceRowIndex > -1 &&
                e.Column.FieldName.StartsWith("S") && e.Column.FieldName.Length == 2)
                if (e.IsGetData)
                {
                    string col = e.Column.FieldName.Replace("S", "Product") + "Id";

                    if (((DataRowView)e.Row)[col].ToInt() > 0)
                    {
                        DataRow[] dr = AppSession.Lookups.V_Kumas.Select("ProductId=" + ((DataRowView)e.Row)[col].ToString());
                        if (dr.Length > 0)
                            if (dr[0]["PhotoMikro"] != DBNull.Value)
                                e.Value = dr[0]["PhotoMikro"];
                    }
                }
        }

    }
}
